'''
Created on Apr 7, 2011

@author: yc30v1m
'''
# used by TL Attachments 
from ConfigParser import ConfigParser
from TestlinkAPIClient import TestlinkAPIClient
from StringIO import StringIO

class TestLinkScriptParser(object):
    '''
    classdocs
    '''
    _testCaseId = ''
    _testCaseResult = ''
    _testCaseScript = ''
    
    _parser = ''
    _api = ''
    

    def __init__(self, devKey, tlServerUrl=None):
        self._parser = ConfigParser()
        self._api = TestlinkAPIClient(devKey, tlServerUrl)
        
    
    def setTCID(self, tcid):
        print '************************* ', tcid, ' ***************************'
        self._postCleanup()
        self._testCaseId = tcid
        self.setupTestScript()
        
    def getAutomationScriptConfigurations(self):
        return self.getSection('PythonAutomation')
    
    def getSectionAndOption(self,section, option):
        if self._parser.has_option(section, option)== True:
            print section, " " , option
            return self._parser.get(section,option)
        return False
    
    def getSection(self,section):
        return self._parser.items(section)
    
    def getTCID(self):
        return self._testCaseId
    
    def setTCResult(self,testResult):
        self._testCaseResult = testResult
        print 'Storing Result'
        self._postResult()
        print '********************************************************' 
        return True
    
    def setupTestScript(self):
        if self._testCaseId == '':
            return False
        temp = self._api.getTestScript(self._testCaseId)
        #Convert the string to FileLike object for ConfigParser
        self._testCaseScript = StringIO(temp)
        self._parseScriptFile()
        return self._testCaseScript
    
    def _postResult(self):
        if self._testCaseId != '':
            if self._testCaseResult != '':
                self._api.reportTCResult(self._testCaseId, self._testCaseResult)
                self._postCleanup()
            else:
                print 'TestCase Result Not Set self._testCaseResult -'
        else:
            print 'Unable to post results as TCID is not set '
            
    def _postCleanup(self):
        self._testCaseId = ''
        self._testCaseResult = ''
        self._testCaseScript = ''
    
    def _parseScriptFile(self):
        self._parser.readfp(self._testCaseScript)
    
    def setProjectName(self, projectName):
        self._api.setProjectName(projectName)        
        
    def setTestPlanName(self,testPlanName):
        self._api.setTestPlanName(testPlanName)
        
    def setBuildName(self,buildName):
        self._api.setBuildName(buildName)
        
    def setAutomationScriptFileName(self,attachedScriptFileName):
        self._api.setAutomationScriptFileName(attachedScriptFileName)
        
    def getTestCaseIdsForTestPlan(self):
        self._api.runAutoSetup()
        return self._api.getTestCaseIdsForTestPlan()
        

#p = TestLinkScriptParser()
#p.setTCID('PSP-9')
#print p.getSectionAndOption('SECTION2','query')
#print p.getSection('SECTION2')
    